package org.daidai.abstrusewriting.homepage.live.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.fragments.BaseFragment;
import org.daidai.abstrusewriting.homepage.live.models.liveItem;
import org.daidai.abstrusewriting.homepage.live.models.liveSmiple;
import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class MyAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<liveItem> list;
    public MyAdapter(Context context, List<liveItem> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        int ret =0;
        if (list != null) {
            ret =list.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret =null;
        if (convertView!=null)
        {
            ret=convertView;
        }
        else
        {
            ret= LayoutInflater.from(context).inflate(R.layout.live_item,parent,false);
        }
        ViewHolder viewHolder = (ViewHolder) ret.getTag();
        if (viewHolder==null)
        {
            viewHolder =new ViewHolder(ret);
            ret.setTag(viewHolder);
        }
        viewHolder.live_pic_0.setOnClickListener(this);
        viewHolder.live_pic_0.setTag(list.get(position).getList().get(0));
        viewHolder.live_pic_1.setOnClickListener(this);
        viewHolder.live_pic_1.setTag(list.get(position).getList().get(1));
        viewHolder.live_pic_2.setOnClickListener(this);
        viewHolder.live_pic_2.setTag(list.get(position).getList().get(2));


        viewHolder.live_title.setText(list.get(position).getTitle());
        Picasso.with(context)
                .load(list.get(position).getList().get(0).getSmallLogo())
                .config(Bitmap.Config.RGB_565)
                .resize(100,100)
               .into(viewHolder.live_pic_0);

        viewHolder.live_name_0.setText(list.get(position).getList().get(0).getNickname());
        viewHolder.live_name_1.setText(list.get(position).getList().get(1).getNickname());
        viewHolder.live_name_2.setText(list.get(position).getList().get(2).getNickname());
        Picasso.with(context)
                .load(list.get(position).getList().get(1).getSmallLogo())
                .config(Bitmap.Config.RGB_565)
                .resize(100,100)
                .into(viewHolder.live_pic_1);
        Picasso.with(context)
                .load(list.get(position).getList().get(2).getSmallLogo())
                .config(Bitmap.Config.RGB_565)
                .resize(100,100)
                .into(viewHolder.live_pic_2);
        return ret;
    }

    @Override
    public void onClick(View v) {
        Object tag = v.getTag();
        if (tag instanceof liveSmiple)
        {
            liveSmiple simple = (liveSmiple) tag;
            EventBus.getDefault().post(simple);
        }

    }

    public class ViewHolder
    {
        private TextView live_title;
        private ImageView live_pic_0;
        private ImageView live_pic_1;
        private ImageView live_pic_2;
        private TextView live_name_0;
        private TextView live_name_1;
        private TextView live_name_2;
        public ViewHolder(View item)
        {
            live_title= (TextView) item.findViewById(R.id.live_title);
            live_pic_0= (ImageView) item.findViewById(R.id.live_pic_0);
            live_pic_1= (ImageView) item.findViewById(R.id.live_pic_1);
            live_pic_2= (ImageView) item.findViewById(R.id.live_pic_2);
            live_name_0= (TextView) item.findViewById(R.id.live_name_0);
            live_name_1= (TextView) item.findViewById(R.id.live_name_1);
            live_name_2= (TextView) item.findViewById(R.id.live_name_2);
        }

    }
}
